#include "OPA.h"
#include "HardWare.h"
#include "UART.h"

void initOPA0(void)
{
    //---- OPA0N 设定 ----//
    _opdsw2 = 1;        //
    _opdsw1 = 0;

    //---- 放大倍数设定 ----//
    //---- 放大倍数设定 ----//
    // OPA0_X1();
    // OPA0_X20();
    // OPA0_X40();
    OPA0_X60();

    //---- 反馈网络设定 ----//
    _opdsw5 = 1;
    _opdsw0 = 0;

    CalibrateOPA0();
    //---- OPA0P 设定 ----//
    _opdsw6 = 1;
    _opdsw7 = 0;


    _opda0ofm = 0;
    _opda0rsp = 0;

    _opda0bw = 1;
    _opda0en = 0;
}



void initOPA1(void)
{
    _opdsw14 = 0;
    _opdsw15 = 1;
    _opdsw19 = 0;

    _opdsw16 = 0;
    _opdsw17 = 1;
    _opdsw18 = 1;

    // _opdsw10 = 1;
    // _opdsw11 = 0;
    // _opdsw12 = 1;
    // _opdsw13 = 0;
    _opdsw10 = 1;
    _opdsw11 = 0;
    _opdsw12 = 1;
    _opdsw13 = 0;

    OPA1_X1();
    // OPA1_X10();
    // OPA1_X20();
    // OPA1_X30();
    // OPA1_X40();
    // OPA1_X50();
    // OPA1_X60();
    // OPA1_X70();

    _opdsw25 = 1;

    // CalibrateOPA1();

    CalibrateOPA1();

    _opda1ofm = 0;
    _opda1rsp = 0;

    _opda1bw = 1;
    _opda1en = 0;

}

void initCMP(void)
{
    vuint8 cal = 0;
    vuint8 voos1,voos2;
    vuint8 cpout;

    _opdcen = 1;

    _opdda = 128;
    
    _opdcofm = 1;
    _opdcrsp = 1;

    _ocpccal &= 0b11100000;
    if(_cpout)  cpout = 1;
    else        cpout = 0;

    while(_cpout == cpout)
    {
        _ocpccal++;
        cal++;
        if(cal >= 15)
            break;
        GCC_DELAY(100);
    }
    voos1 = _ocpccal & 0b00011111;
    // UART_SendByte(voos1);

    cal = 0;
    _ocpccal |= 0b00011111;
    if(_cpout)  cpout = 1;
    else        cpout = 0;
    while(_cpout == cpout)
    {
        _ocpccal--;
        cal++;
        if(cal >= 15)
            break;
        GCC_DELAY(100);
    }
    voos2 = _ocpccal & 0b00011111;
    // UART_SendByte(voos2);

    cal = (voos1 + voos2)/2;
    _ocpccal = cal;
    // UART_SendByte(_ocpccal & 0b00111111);

    _opdcofm = 1;
    _opdcen = 0;

}


void CalibrateOPA0(void)
{    
    vuint8 cal = 0;
    vuint8 voos1,voos2;
    // volatile bit f_output;

    _opda0bw = 1;
    _opda0en = 1;

    _opda0ofm = 1;
    _opda0rsp = 1;

    SEL_PA7_OPA0O();
    _opda0cal &= 0b11000000;
    GCC_DELAY(1000);
#if 1
    while(_pa7 == 0)
    {
        cal++;
        if(cal < 64)
        {
            _opda0cal++;
        }
        else
            break;
        GCC_DELAY(100);
    }
#else
    GCC_DELAY(100);
    if(_pa7)    f_output = 1;
    else        f_output = 0;
    while(_pa7 == f_output)
    {
        _opda0cal++;
        cal++;
        if(cal >= 63)
            break;
        GCC_DELAY(100);
    }
#endif

    voos1 = _opda0cal & 0b00111111;
    // UART_SendByte(voos1);


#if 1
    cal = 63;
    _opda0cal |= 0b00111111;
    GCC_DELAY(1000);
    while(_pa7)
    {
        if(cal)
        {
            cal--;
            _opda0cal--;

            if(cal == 0)
                break;
        }
        GCC_DELAY(100);
    }
#else
    cal = 0;
    _opda0cal |= 0b00111111;
    GCC_DELAY(100);
    if(_pa7)    f_output = 1;
    else        f_output = 0;
    while(_pa7 == f_output)
    {
        _opda0cal--;
        cal++;
        if(cal >= 63)
            break;
        GCC_DELAY(100);
    }
#endif

    voos2 = _opda0cal & 0b00111111;
    // UART_SendByte(voos2);

    cal = (voos1 + voos2)/2;
    _opda0cal = cal;
    // UART_SendByte(_opda0cal & 0b00111111);

    // UART_SendByte(0x55);

    // _opda0bw = 0;
    _opda0rsp = 0;
    _opda0en = 0;
}

void CalibrateOPA1(void)
{
    vuint8 cal = 0;
    vuint8 voos1,voos2;
    // volatile bit f_output;

    _opdsw18 = 0;

    _opda1bw = 1;
    _opda1en = 1;

    _opda1ofm = 1;
    _opda1rsp = 1;

    SEL_PA1_OPA1O();
    _opda1cal &= 0b11000000;
    GCC_DELAY(1000);

#if 1
   while(_pa1 == 1)
//    while(_pa1 == 0)
    {
        cal++;
        if(cal < 64)
        {
            _opda1cal++;
        }
        else
            break;
        GCC_DELAY(100);
    }
#else
    if(_pa1)    f_output = 1;
    else        f_output = 0;
    while(_pa1 == f_output)
    {
        _opda1cal++;
        cal++;
        if(cal >= 63)
            break;
        GCC_DELAY(100);
    }
#endif
    voos1 = _opda1cal & 0b00111111;
    // UART_SendByte(voos1);

#if 1
    cal = 63;
    _opda1cal |= 0b00111111;
    GCC_DELAY(1000);
    while(_pa1 == 0)
    // while(_pa1 == 1)
    {
        if(cal)
        {
            cal--;
            _opda1cal--;

            if(cal == 0)
                break;
        }
        GCC_DELAY(100);
    }
#else
    cal = 0;
    _opda1cal |= 0b00111111;
    GCC_DELAY(100);
    if(_pa1)    f_output = 1;
    else        f_output = 0;
    while(_pa1 == f_output)
    {
        _opda1cal--;
        cal++;
        if(cal >= 63)
            break;
        GCC_DELAY(100);
    }
#endif
    voos2 = _opda1cal & 0b00111111;
    // UART_SendByte(voos2);

    cal = (voos1 + voos2)/2;
    _opda1cal = cal;
    // UART_SendByte(_opda1cal & 0b00111111);
    // UART_SendByte(0xAA);

    _opda1rsp = 0;
    // _opda1bw = 0;
    _opda1en = 0;

    
    _opdsw18 = 1;
}